package com.zang.everyday;

import java.util.HashSet;

/**
 * @author ZVerify
 * @since 2022/10/12 08:28
 * @see <a href="https://leetcode.cn/problems/linked-list-components/submissions/">...</a>
 **/
public class 链表组件 {

    public int numComponents(ListNode head, int[] nums) {

        HashSet<Integer> set = new HashSet<>();

        for (int num : nums) {
            set.add(num);
        }

        ListNode cur = head;

        int sum = 0;

        while (cur!=null){
            if (set.contains(cur.val) && (cur.next==null || !set.contains(cur.next.val))) sum++;
            cur = cur.next;
        }

        return sum;

    }
}
